#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
翻译API简单测试客户端
"""

import requests
import json

def test_translation():
    """测试翻译功能"""
    api_url = "http://localhost:8000/translate"
    
    # 测试数据
    test_cases = [
        "炉膛内壁温度",
        "辐射管表面温度", 
        "喷淋水流量",
        "燃气压力调节阀",
        "助燃空气流量计",
        "雾化器压力",
        "冷却水温度",
        "烧嘴出口温度"
    ]
    
    print("=== 翻译API测试 ===")
    print(f"API地址: {api_url}")
    print("-" * 60)
    
    for i, text in enumerate(test_cases, 1):
        try:
            payload = {
                "text": text,
                "from_lang": "zh", 
                "to_lang": "en"
            }
            
            print(f"{i:2d}. 原文: {text}")
            
            response = requests.post(api_url, json=payload, timeout=30)
            
            if response.status_code == 200:
                result = response.json()
                print(f"    翻译: {result['translated_text']}")
                print(f"    简化: {result['simplified_name']}")
                print()
            else:
                print(f"    ❌ 错误: HTTP {response.status_code}")
                print(f"    详情: {response.text}")
                print()
                
        except requests.exceptions.RequestException as e:
            print(f"    ❌ 网络错误: {e}")
            print()
        except Exception as e:
            print(f"    ❌ 其他错误: {e}")
            print()

def check_api_health():
    """检查API健康状态"""
    try:
        response = requests.get("http://localhost:8000/health", timeout=5)
        if response.status_code == 200:
            health = response.json()
            print("✅ API服务正常运行")
            print(f"   模型已加载: {health.get('model_loaded', False)}")
            print(f"   GPU可用: {health.get('gpu_available', False)}")
            return True
        else:
            print(f"❌ API服务异常: {response.status_code}")
            return False
    except Exception as e:
        print(f"❌ 无法连接到API服务: {e}")
        print("💡 请确保翻译API服务正在运行:")
        print("   python translation_api.py")
        return False

if __name__ == "__main__":
    print("🔍 检查API服务状态...")
    if check_api_health():
        print()
        test_translation()
        print("🎉 测试完成!")
    else:
        print("\n🚀 启动API服务:")
        print("   python translation_api.py")
        print("或运行完整测试:")
        print("   python start_translation_system.py")